<!--<template>-->
    <!--<div class="login">-->
        <!--<ul>-->
            <!--<li><label>帐号：</label><input type="text"></li>-->
            <!--<li><label>密码：</label><input type="text"></li>-->
        <!--</ul>-->
    <!--</div>-->
<!--</template>-->

<!--<script type="text/ecmascript-6">-->
    <!--import 'components/login/login.css'-->
    <!--export default {}-->
<!--</script>-->

<!--<style>-->

<!--</style>-->


<!--<template>-->
  <!--<div class="login">-->

    <!--&lt;!&ndash;v.model.trim 去掉首尾的空格&ndash;&gt;-->
    <!--&lt;!&ndash;@click.submit.prevent 阻止事件的重复提交&ndash;&gt;-->

    <!--<form class="form" >-->
      <!--<input type="text"  class="input" name="name" v-model.trim='formData.name' placeholder="姓名"><br>-->
      <!--<input type="text"  class="input" name="phone" v-model.trim='formData.phone' placeholder="手机号"><br>-->
      <!--<button class="input" type="submit" @click="loginHandler(formData)">登录</button>-->
    <!--</form>-->
  <!--</div>-->
<!--</template>-->
<!--<script>-->
  <!--export default {-->
    <!--name: 'login',-->
    <!--data(){-->
      <!--return {-->
        <!--formData: {-->
          <!--name: '',-->
          <!--phone: ''-->
        <!--}-->
      <!--}-->
    <!--},-->
  <!--}-->
<!--</script>-->
<!--<style>-->
  <!--.login {-->
    <!--min-height: 100vh;-->
    <!--width: 100%;-->
  <!--}-->
  <!--.form {-->
    <!--display: flex;-->
    <!--align-items: center;-->
    <!--justify-content: center;-->
  <!--}-->
  <!--.input {-->
    <!--width:300px;-->
    <!--height: 80px;-->
  <!--}-->
<!--</style>-->






<template>
  <div class="login-container">
    <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left"
             label-width="0px"
             class="card-box login-form">
      <h3 class="title">登录</h3>
      <el-form-item prop="name">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user"/>
        </span>
        <el-input name="name" disabled type="text" v-model="loginForm.name" autoComplete="on"
                  placeholder="用户名"/>
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password"
                  autoComplete="on"
                  placeholder="密码"></el-input>
        <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye"/></span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: 'login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (value.trim().length<1) {
          callback(new Error('用户名不能为空'))
        } else {
          callback()
        }
      };
      const validatePass = (rule, value, callback) => {
        if (value.trim().length < 1) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      };
      return {
        loginForm: {
          name: '',
          password: ''
        },
        loginRules: {
          name: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePass}]
        },
        loading: false,
        pwdType: 'password'
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            this.$store.dispatch('Login', this.loginForm).then(() => {
              this.loading = false;
              this.$router.push({path: '/'});
            }).catch((e) => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>
